package df.servlet.process;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import df.bean.obj.util.JDate;
import df.bean.process.ProcessComposit;

/**
 * Servlet implementation class ProcessCompositeSrvl
 */
public class ProcessCompositeSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	
	public ProcessCompositeSrvl() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/xml; charset=UTF-8");

		response.getWriter().print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

		request.setCharacterEncoding("UTF-8");

		 int numAffRec = 0;
		String hospital_code = (String) request.getSession().getAttribute("HOSPITAL_CODE");
		String date_start = JDate.saveDate(request.getParameter("date_start"));
		String date_end = JDate.saveDate(request.getParameter("date_end"));

		try{ 
			ProcessComposit obj_composite = new ProcessComposit();
			if (obj_composite.calculateComposite(hospital_code ,  date_start , date_end)) {

				 numAffRec = 1;
			} 
		} catch(Exception ex){ 
			response.getWriter().print(ex);
		}	
		
		  try {
			Thread.sleep(20);
			response.getWriter().print("<RESULT><SUCCESS>" + numAffRec + "</SUCCESS></RESULT>");
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
	}
}
